package com.ethan.blog.common.result;

import com.ethan.blog.common.constant.MessageConstant;
import com.ethan.blog.common.constant.StatusCodeConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 后端同意返回结果
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    private Integer code; // 响应状态码 0表示错误 1表示正常
    private String message; // 响应消息
    private T data; // 返回数据

    public static <T> Result<T> success() {
        return success((T) null);
    }

    public static <T> Result<T> success(T data) {
        return success(MessageConstant.SUCCESS, data);
    }

    public static <T> Result<T> success(String msg) {
        return success(msg, (T) null);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(StatusCodeConstant.SUCCESS_CODE, msg, data);
    }

    public static <T> Result<T> error() {
        return error(MessageConstant.ERROR);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<T>(StatusCodeConstant.ERROR_CODE, msg, null);
    }

    public static <T> Result<T> error(T data) {
        return new Result<T>(StatusCodeConstant.ERROR_CODE, null, data);
    }
}
